/************************************************************************

							Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  							   ptr_cache.h

	$Header: /resource_editor/ptr_cache.h $

	$NoKeywords: $

 ************************************************************************/
#ifndef PTR_CACHE_H_INCLUDED
#define PTR_CACHE_H_INCLUDED

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "abstract_resource_cache.h"
#include "owned_ptr.h"

// -------------------------------------------------------------------
// cached resource object template.  Used to hold the relevant pointer.
// -------------------------------------------------------------------
template < typename type >
class t_ptr_cache_data : public t_abstract_resource_cache_data< type >
{
public:
	// Constructor/Destructor
	t_ptr_cache_data( std::string const & name );

private:
	// Member functions
	virtual char const * get_prefix() const;
	virtual type * do_read( std::streambuf & stream, t_progress_handler* handler );
};

template < typename type >
inline t_ptr_cache_data< type >::t_ptr_cache_data( std::string const & name )
	:	t_abstract_resource_cache_data< type >( name )
{
}

template < typename type >
char const * t_ptr_cache_data< type >::get_prefix() const
{
	return t_resource_traits< type >::prefix;
}

template < typename type >
type * t_ptr_cache_data< type >::do_read( std::streambuf & stream, t_progress_handler* handler )
{
	t_owned_ptr< type > result( new type );
	if ( !::read( stream, *result ) )
		result.reset();
	return result.release();
}

// -------------------------------------------------------------------
// cached resource object template.  Used to hold the relevant pointer.
// -------------------------------------------------------------------
template <typename type>
class t_pointer_cache : public t_abstract_cache<type>
{
public:
	t_pointer_cache();
	t_pointer_cache( std::string const& name );
};

template <typename type>

inline t_pointer_cache<type>::t_pointer_cache() : t_abstract_cache<type>( 0 )
{
}

template <typename type>
inline t_pointer_cache<type>::t_pointer_cache( std::string const& name )
							: t_abstract_cache<type>( new t_ptr_cache_data<type>( name ) )
{
}

#endif
